package meijuli.com.join51.business.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import com.jhx.common.http.IPServFactory;
import com.jhx.common.framework.QuickBaseActivity;
import com.jhx.common.http.ApiManager;
import com.jhx.common.utils.ToastUtil;

/**
 * 设置环境
 *
 * @author wenqin 2017-05-15 20:02
 */

public class IpActivity extends QuickBaseActivity {
    private static final int STATUS_PRODUCT = 1;
    private static final int STATUS_TEST = 2;
    private static final int STATUS_LOCAL = 3;
    private static final int STATUS_CUSTOM = 4;
    TextView mCurrentIp;
    TextView mProductIp;
    TextView mTestIp;
    TextView mLocalIp;
    LinearLayout mEditLayout;
    EditText mEditIp;
    EditText mEditNewIp;
    CheckBox mHttpCheckBox;

    private IPServFactory ipServ;
    private int currentStatus = -1;
    private String http = "http";
    private String https;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ip);
        mCurrentIp = findViewById(R.id.current_ip);
        mProductIp = findViewById(R.id.product_ip);
        mTestIp = findViewById(R.id.test_ip);
        mLocalIp = findViewById(R.id.local_ip);
        mEditLayout = findViewById(R.id.edit_layout);
        mEditIp = findViewById(R.id.edit_ip);
        mEditNewIp = findViewById(R.id.edit_new_ip);
        mHttpCheckBox = findViewById(R.id.http_check_box);

        initData();
        initToolBar();
        initListener();
    }

    private void initListener() {
        mProductIp.setOnClickListener(new ClickListener(STATUS_PRODUCT));
        mTestIp.setOnClickListener(new ClickListener(STATUS_TEST));
        mLocalIp.setOnClickListener(new ClickListener(STATUS_LOCAL));
        mHttpCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                currentStatus = isChecked ? STATUS_CUSTOM : currentStatus;
                mEditLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
        });
    }

    private void initData() {
        ipServ = IPServFactory.sharedInstance();
        https = ipServ.getSchemeOfHttps();

        setCurrentIp();
        setupProductIp();
        setupTestIp();
        setupLocalIp();
    }

    private void initToolBar() {
        showToolbar();
        setTitle("设置环境");
        setMenuText("确定");
    }

    @Override
    public void onNavigationClick(View view) {
        super.onNavigationClick(view);
        finish();
    }

    @Override
    protected void onMenuTextClick(View view) {
        super.onMenuTextClick(view);
        rightBtnClick();
    }

    private void rightBtnClick() {
        switch (currentStatus) {
            case STATUS_PRODUCT:
                ipServ.setIp(ipServ.getSchemeOfHttps() + ipServ.getProductEnv());
                ipServ.setNewIp(ipServ.getSchemeOfHttps() + ipServ.getNewProductEnv());
                break;
            case STATUS_TEST:
                ipServ.setIp(ipServ.getSchemeOfHttps() +ipServ.getTestEnv());
                ipServ.setNewIp(ipServ.getSchemeOfHttps() +ipServ.getNewTestEnv());
                break;
            case STATUS_LOCAL:
                ipServ.setIp(ipServ.getScheme() +ipServ.getLocalEnv());
                ipServ.setNewIp(ipServ.getScheme() +ipServ.getNewLocalEnv());
                break;
            case  STATUS_CUSTOM:
                String ipStr = mEditIp.getText().toString();
                String newIpStr = mEditNewIp.getText().toString();
                if (mHttpCheckBox.isChecked()) {
                    if (!TextUtils.isEmpty(ipStr) && !TextUtils.isEmpty(newIpStr)) {
                        if (!ipStr.contains(http) || !newIpStr.contains(http)) {
                            if (ipStr.contains(https) || newIpStr.contains(https)) {
                                ToastUtil.getInstance().showToast(this, "此模式下不能自定义https环境");
                            } else {
                                ToastUtil.getInstance().showToast(this, "请填写完整域名环境");
                            }
                            return;
                        } else {
                            ipServ.setIp(ipStr);
                            ipServ.setNewIp(newIpStr);
                        }
                    } else {
                        ToastUtil.getInstance().showToast(this, "资料请填写完整");
                        return;
                    }
                } else {
                    ipServ.setIp(ipServ.getServerName());
                    ipServ.setNewIp(ipServ.getNewServerName());
                }
                break;
            default:

                break;
        }
        ApiManager.clear();
    }

    private class ClickListener implements View.OnClickListener {

        private int status;

        ClickListener(int status) {
            this.status = status;
        }

        @Override
        public void onClick(View v) {
            String currentStr = "";
            String currentNewStr = "";
            switch (status) {
                case STATUS_PRODUCT:
                    currentStr = ipServ.getSchemeOfHttps() + ipServ.getProductEnv();
                    currentNewStr = ipServ.getSchemeOfHttps() + ipServ.getNewProductEnv();
                    break;
                case STATUS_TEST:
                    currentStr = ipServ.getSchemeOfHttps() + ipServ.getTestEnv();
                    currentNewStr = ipServ.getSchemeOfHttps() + ipServ.getNewTestEnv();
                    break;
                case STATUS_LOCAL:
                    currentStr = ipServ.getScheme() + ipServ.getLocalEnv();
                    currentNewStr = ipServ.getScheme() + ipServ.getNewLocalEnv();
                    break;
                default:

                    break;
            }
            mCurrentIp.setText("当前环境：" + currentStr
                    + "\n新接口环境：" + currentNewStr);
            currentStatus = status;
        }
    }

    private void setupLocalIp() {
        mLocalIp.setText("本地环境：\n" + ipServ.getScheme() + ipServ.getLocalEnv()
                + "\n新接口本地环境：\n" + ipServ.getScheme() + ipServ.getNewLocalEnv());
    }

    private void setupTestIp() {
        mTestIp.setText("测试环境：\n" + https + ipServ.getTestEnv()
                + "\n新接口测试环境：\n" + https + ipServ.getNewTestEnv());
    }

    private void setupProductIp() {
        mProductIp.setText("正式环境：\n" + https + ipServ.getProductEnv()
                + "\n新接口正式环境：\n" + https + ipServ.getNewProductEnv());
    }

    public void setCurrentIp() {
        mCurrentIp.setText("当前环境：" + ipServ.getServerName()
                + "\n新接口环境：" + ipServ.getNewServerName());

        mEditIp.setText(ipServ.getServerName());
        mEditNewIp.setText(ipServ.getNewServerName());
        mEditIp.setSelection(mEditIp.getText().length());
    }
}
